package com.baidu.ueditor.upload;

import com.baidu.ueditor.define.AppInfo;
import com.baidu.ueditor.define.BaseState;
import com.baidu.ueditor.define.State;
import com.framework.entity.sys.SysUserEntity;
import com.framework.exception.RRException;
import com.framework.modules.oss.cloud.OSSFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class BinaryUploader {

	/*public static final State save(HttpServletRequest request,
        Map<String, Object> conf) {
FileItemStream fileStream = null;
    boolean isAjaxUpload = request.getHeader( "X_Requested_With" ) != null;

    if (!ServletFileUpload.isMultipartContent(request)) {
        return new BaseState(false, AppInfo.NOT_MULTIPART_CONTENT);
    }

    ServletFileUpload upload = new ServletFileUpload(
            new DiskFileItemFactory());

    if ( isAjaxUpload ) {
        upload.setHeaderEncoding( "UTF-8" );
    }

    try {
        FileItemIterator iterator = upload.getItemIterator(request);

        while (iterator.hasNext()) {
            fileStream = iterator.next();

            if (!fileStream.isFormField())
                break;
            fileStream = null;
        }

        if (fileStream == null) {
            return new BaseState(false, AppInfo.NOTFOUND_UPLOAD_DATA);
        }

        String savePath = (String) conf.get("savePath");
        String originFileName = fileStream.getName();
        String suffix = FileType.getSuffixByFilename(originFileName);

        originFileName = originFileName.substring(0,
                originFileName.length() - suffix.length());
        savePath = savePath + suffix;

        long maxSize = ((Long) conf.get("maxSize")).longValue();

        if (!validType(suffix, (String[]) conf.get("allowFiles"))) {
            return new BaseState(false, AppInfo.NOT_ALLOW_FILE_TYPE);
        }

        savePath = PathFormat.parse(savePath, originFileName);

        String physicalPath = (String) conf.get("rootPath") + savePath;

        InputStream is = fileStream.openStream();
        State storageState = StorageManager.saveFileByInputStream(is,
                physicalPath, maxSize);
        is.close();

        if (storageState.isSuccess()) {
            storageState.putInfo("url", PathFormat.format(savePath));
            storageState.putInfo("type", suffix);
            storageState.putInfo("original", originFileName + suffix);
        }

        return storageState;
    } catch (FileUploadException e) {
        return new BaseState(false, AppInfo.PARSE_REQUEST_ERROR);
    } catch (IOException e) {
    }
    return new BaseState(false, AppInfo.IO_ERROR);


}*/
	//重写了上传方法,用于将图片上传至阿里云
	public static final State save(HttpServletRequest request,Map<String, Object> conf) {

		try {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile file = multipartRequest.getFile(conf.get("fieldName").toString());
			SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
			Long deptId=user.getDeptId();
			String prefix="";
			if(deptId.equals(1L)){
				prefix="banner";
			}else if(deptId.equals(889323L)){
				prefix="kayi";
			}else if(deptId.equals(889324L)){
				prefix="xinchuang";
			}else if(deptId.equals(889325L)){
				prefix="suchuangbang";
			}else if(deptId.equals(889326L)) {
				prefix="kaola";
			}else {
				prefix="banner";
			}

			if (file == null) {
				throw new RRException("9000","上传文件不能为空");
			}
			if (file.isEmpty()) {
				throw new RRException("9000","上传文件不能为空");
			}

			// 上传文件
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix,prefix);

			State storageState =new BaseState(true);
			storageState.putInfo("url", url);
			storageState.putInfo("type", suffix);
			storageState.putInfo("original", file.getOriginalFilename());

			return storageState;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return new BaseState(false, AppInfo.IO_ERROR);
	}
	/*private static boolean validType(String type, String[] allowTypes) {
		List<String> list = Arrays.asList(allowTypes);

		return list.contains(type);
	}*/
}
